<!-- 面试查询 -->
<script>
import HPCommonPage from "@/components/HPCommonPage";
import HPTable from "@/components/HPTable";
import { receiveAnOrder } from "@/fetch/modules/quoted";
import { orderTableColumn } from "../js/cloumn";
import { orderStatus } from "../js/status";
import { debounce } from 'lodash';
import getQuery from '@/utils/mixins/getQuery';

function fetchQuotedLists(store, opts) {
  return store.dispatch("GET_QUOTED_LISTS", opts);
}
export default {
  mixins: [getQuery],
  created() {
    const { stus } = this.$route.query;
    if (stus) {
      this.selectData.state = Number(stus);
    }
    this.fetchTableData();
  },
  methods: {
    // * 格式化接口参数
    formatQueryData() {
      const {
        needsCode,
        postCode,
        name,
        createDate,
        state,
        pageCurrent,
        pageSize,
      } = this.selectData;
      const [interviewTime_b, interviewTime_e] = createDate || [];
      return {
        needsCode,
        postCode,
        name,
        interviewTime_b,
        interviewTime_e,
        state,
        pageCurrent,
        pageSize,
      };
    },
    // * 获取列表数据
    async fetchTableData() {
      this.isLoad = true;
      await fetchQuotedLists(this.$store, this.formatQueryData());
      this.isLoad = false;
      const { state, data } = this.$store.getters.getQuotedLists;
      if (state === "0000") {
        const { list = [], total, lastPage, pageNum } = data || {};
        this.tableData = list || [];
        this.totals = total;
        if(pageNum > lastPage && lastPage > 0) {
          this.selectData.pageCurrent = lastPage
          this.fetchTableData()
        }
      }
    },
    // * 查询
    onSearch() {
      this.selectData.pageCurrent = 1;
      this.searchData = { ...this.selectData }
      this.fetchTableData();
    },
    // * 接收
    onReceiveInterView: debounce(
      async function(row) {
      const { needsCvId, offerId, needs_code } = row;
      const { data } = await receiveAnOrder({
        needsCvId,
        offerId,
        needs_code,
        state: 1,
      });

      if (data.state === "0000") {
        this.fetchTableData();
        this.$message.success("成功");
      }
    }, 1000),
    // * 拒绝
    onRejectInterView(row) {
      const _this = this;
      const { needsCvId, offerId, needs_code } = row;
      this.$confirm("需求拒绝后，无法撤销！")
        .then(async (_) => {
          const { data } = await receiveAnOrder({
            needsCvId,
            offerId,
            needs_code,
            state: 3,
          });

          if (data.state === "0000") {
            _this.fetchTableData();
            _this.$message.success("成功");
          }
          done();
        })
        .catch((_) => {
          console.log(_);
        });
    },
    // * 查看
    onLookDetail({ needsCvId, state, offerId, offerType }) {
      this.$router.push({
        path: `/quoted/detail/${needsCvId}`,
        query: {
          stus: state,
          ofer: offerId,
          ot: offerType,
          num: 100,
          formQuery: JSON.stringify(this.searchData),
        },
      });
    },
    // * 切换页条数
    handleSizeChange(val) {
      this.selectData.pageSize = val;
      this.searchData = { ...this.selectData }
      this.onSearch();
    },
    // * 切换页数
    handleCurrentChange(val) {
      this.selectData.pageCurrent = val;
      this.searchData = { ...this.selectData }
      this.fetchTableData();
    },
  },
  data() {
    return {
      selectData: {
        // * 条件查询对象
        state: "",
        postCode: "",
        needsCode: "",
        name: "",
        createDate: [],
        pageCurrent: 1,
        pageSize: 10,
      },
      statusOptions: orderStatus,
      tableHeaderItems: orderTableColumn,
      approverStatus: orderStatus,
      tableData: [],
      isLoad: false,
      totals: 0,
    };
  },
  components: {
    HPCommonPage,
    HPTable,
  },
};
</script>

<template>
  <HPCommonPage :titleText="'面试管理 / 订单确认'">
    <template v-slot:searchForm>
      <el-form :inline="true" :model="selectData" class="form-box res-form-box">
        <el-form-item label="需求单号：">
          <el-input
            v-model="selectData.needsCode"
            clearable
            placeholder="请输入需求单号"
          ></el-input>
        </el-form-item>
        <el-form-item label="岗位名称：">
          <el-input
            v-model="selectData.postCode"
            clearable
            placeholder="请输入岗位名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="面试时间：" :class="'date-font-item'">
          <el-date-picker
            v-model="selectData.createDate"
            size="small"
            type="daterange"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="姓名：">
          <el-input
            v-model="selectData.name"
            clearable
            placeholder="请输入输入姓名"
          ></el-input>
        </el-form-item>
        <el-form-item label="订单接收状态：">
          <el-select
            v-model="selectData.state"
            placeholder="请选择订单接收状态"
            clearable=""
          >
            <el-option
              v-for="item in statusOptions"
              :key="item.key"
              :label="item.label"
              :value="item.key"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="订单接收状态：">
          <el-select
            v-model="selectData.approvalState"
            placeholder="请选择订单接收状态"
            clearable=""
          >
            <el-option
              v-for="item in approverStatus"
              :key="item.key"
              :label="item.label"
              :value="item.key"
            >
            </el-option>
          </el-select>
        </el-form-item> -->
      </el-form>
    </template>
    <template v-slot:searchFormbtn>
      <el-button type="success" size="small" @click="onSearch">查询</el-button>
    </template>
    <template v-slot:table>
      <HPTable
        :tableHeaderItems="tableHeaderItems"
        :tableData="tableData"
        :loading="isLoad"
      >
      </HPTable>
    </template>
    <template v-slot:pagination>
      <el-pagination
        v-if="totals"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="selectData.pageCurrent"
        :page-sizes="[10, 20, 30, 40, 50]"
        :page-size="selectData.pageSize"
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="totals"
      >
      </el-pagination>
    </template>
  </HPCommonPage>
</template>

<style lang="scss"></style>
